package org.jeecg.modules.ordercommon;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.customer.entity.BusCustomer;
import org.jeecg.modules.customer.service.IBusCustomerService;
import org.jeecg.modules.demo.clock.entity.BusPointsClock;
import org.jeecg.modules.demo.clock.service.IBusPointsClockService;
import org.jeecg.modules.demo.fapiao.entity.BusFapiao;
import org.jeecg.modules.demo.fapiao.service.IBusFapiaoService;
import org.jeecg.modules.demo.totalpoints.entity.BusVipPoint;
import org.jeecg.modules.demo.totalpoints.service.IBusVipPointService;
import org.jeecg.modules.order.entity.BusOrder;
import org.jeecg.modules.order.service.IBusOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "统计信息")
@RestController
@RequestMapping("/common/static")
@Slf4j
public class BusCommonController {
    @Autowired
    IBusFapiaoService fapiaoService;
    @Autowired
    IBusOrderService orderService;
    @Autowired
    IBusVipPointService pointService;
    @Autowired
    IBusPointsClockService clockService;

    @Autowired
    private IBusCustomerService customerService;

    @GetMapping(value = "/base")
    public Result<?> countInfo() {
        Map<String, Object> result = new HashMap<>();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
//        String userId="e9ca23d68d884d4ebb19d07889727dae";
        QueryWrapper<BusPointsClock> clockQueryWrapper = new QueryWrapper<>();
        clockQueryWrapper.eq("create_by", userId)
                .eq("DATE_FORMAT(create_time,'%Y-%m-%d')", new SimpleDateFormat("YYYY-MM-dd").format(new Date()));
        int count = clockService.count(clockQueryWrapper);
        result.put("clock", count >= 1 ? true : false);
        result.put("fapiao", fapiaoService.list(new QueryWrapper<BusFapiao>().eq("user_id", userId)));
        result.put("points", pointService.getOne(new QueryWrapper<BusVipPoint>().eq("user_id", userId)));
        Map<String, Object> params = new HashMap<>();
        LambdaQueryWrapper<BusCustomer> customerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        customerLambdaQueryWrapper.eq(BusCustomer::getSysUserId, userId);
        BusCustomer busCustomer = customerService.getOne(customerLambdaQueryWrapper);
        if (busCustomer.getManagerType() == 0) {
            params.put("cusId", busCustomer.getId());
        }

        Map<Integer, Integer> maps = orderService.countStatus(params);
        result.put("orderInfo", maps);
        return Result.OK(result);

    }
}
